/*	Author: Aaron Daniels
	File Name: Help.c
	Purpose: Source file for the Help module of the MPX system.
	Last update: 09/13/2011	(Nathan)
*/

#include "mpx_supt.h"
#include "commhand.h"
#include "Help.h"
#include <errno.h>
 
helpItem commands[NUM_COMS];

int initFlag = 0;

int getHelp (char args[MAX_NARGS][MAX_ARGLEN], int nargs){
	int i,j;
	int check = 0;
	
	if(nargs<1) return 0;
	
	//Checks to see if the initialization process has run. It will only run once.
	if(!initFlag){ 
		helpInit(); //initialize commands' names and filepaths
		initFlag = 1;
	}
	
	//check that the first argument is one of the help aliases
	for (i = 0; i < MAX_NUM_NAMES; i++){
		if (strcmp(args[0], commands[0].names[i])==0) check = 1;
	}
	if(check!=1) return 0;
	
	//check correct num args
	if(nargs>2) {
		printf("Error: Improper syntax for command HELP\nProper usage: HELP [<command name>]\n");
		return 1;
	}
	
	//If only one argument, print the general help file
	if (nargs == 1) {
		printCommands();
		return 1;
	}
	else { 
		//recognize appropriate help file, if possible		
		for (i = 0; i < NUM_COMS; i++){
			for (j = 0; j  < MAX_NUM_NAMES; j++){
				if (strcmp(commands[i].names[j], args[1])==0){
					fileDisplay(commands[i].filePath);
					return 1;
				}	
			}
		}		
	}
	printf("Error: No help file available for command '%s'\n",args[1]);
	return 1;
}

void helpInit(){
	int i;
	
	//Name recognition section.
	strcpy(commands[0].names[0],"HELP");
	strcpy(commands[0].names[1],"?");	
	
	strcpy(commands[1].names[0],"CHDATE");
	
	strcpy(commands[2].names[0],"DATE");
	
	strcpy(commands[3].names[0],"DIR");
	strcpy(commands[3].names[1],"DIRECTORY");
	strcpy(commands[3].names[2],"LS");
	
	strcpy(commands[4].names[0],"EXIT");
	strcpy(commands[4].names[1],"QUIT");
	
	strcpy(commands[5].names[0],"VERSION");
	
	strcpy(commands[6].names[0],"SHOWALL");
	
	strcpy(commands[7].names[0],"SHOW");
	
	strcpy(commands[8].names[0],"BLOCK");
	
	strcpy(commands[9].names[0],"UNBLOCK");
	
	strcpy(commands[10].names[0],"RESUME");
	
	strcpy(commands[11].names[0],"SUSPEND");
	
	strcpy(commands[12].names[0],"DELETE");
	
	strcpy(commands[13].names[0],"CHPRIORITY");
	
	strcpy(commands[14].names[0],"CREATE");
	
	strcpy(commands[15].names[0],"LOADR3");
	
	strcpy(commands[16].names[0],"DISPATCH");
	
	strcpy(commands[17].names[0],"LOAD");
	
	strcpy(commands[18].names[0],"TERMINATE");
	//Filepath creation section.
	for (i = 0; i < NUM_COMS; i++){
		//If a filename is longer than 7 characters, it creates a recognition problem.
		if (strlen(commands[i].names[0]) > 7){
			strncpy(commands[i].filePath,commands[i].names[0],7);
		}
		else{
			strcpy(commands[i].filePath,commands[i].names[0]);
		}
		strcat(commands[i].filePath,".txt");
	}
}

void fileDisplay(char *filePath){
	FILE *textFile = fopen(filePath, "r");
	char fromFile[BUFF_SIZE];
	if(textFile == NULL) {
		printf("Error: help file (%s) has yet to be created...\n",filePath);
		printf("The error is... %s",strerror(errno));
		textFile = fopen("version.txt","r");
		if(textFile==NULL) printf("2nd attempt failed\n");
	}	
	while(fgets(fromFile, BUFF_SIZE, textFile) != NULL){
		printf(fromFile);
	}
	printf("\n");
	fclose(textFile);		
}

void printCommands(){
	int i;
	printf("\nList of Commands:\n\n");
	for(i=0; i<NUM_COMS; i++){
		printf("%s\n",commands[i].names[0]);
	}
	printf("For more information about a particular command, type\nHELP <command name>\n\n");
}